package com.example.contact;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ReadCont  extends AppCompatActivity {

    private ContentResolver contentResolver,resolver;
    Cursor phone;
    private ListView lv_listview_person;
    StringBuilder sd;
    //联系人
    private List<Map<String, Object>> persones;
    private SimpleAdapter personsa;
    private Map<String, Object> personmap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.display);
        lv_listview_person = findViewById(R.id.real);

        readperson();
        contentResolver = getContentResolver();
        persones = new ArrayList<Map<String, Object>>();
        //运用了简单适配器
       // personsa = new SimpleAdapter(this, persones,android.R.layout.simple_list_item_1, new String[]{"names", "phones"}, new int[]{android.R.id.text1, android.R.id.text2});
        personsa = new SimpleAdapter(this, persones, R.layout.contact, new String[]{"names", "phones"}, new int[]{R.id.v1, R.id.v2});
        lv_listview_person.setAdapter(personsa);


    }


    //获取联系人列表的方法
    public void readperson() {

        //协议：content://com.android.contacts/raw_contacts（获得联系人的id，姓名）
        Cursor cursor = contentResolver.query(Uri.parse("content://com.android.contacts/raw_contacts"), null, null, null, null);
        while (cursor.moveToNext()) {
            personmap = new HashMap<String, Object>();
            //得到的联系人的id
            int id = cursor.getInt(cursor.getColumnIndex("_id"));
            //得到联系人的名字
            String displayname = cursor.getString(cursor.getColumnIndex("display_name"));
            //把名字添加到集合中
            personmap.put("names", id);
            personmap.put("names", displayname);
            //将联系人添加到集合中
            persones.add(personmap);

        }
        //通知适配器发生改变
        personsa.notifyDataSetChanged();
    }
public  void readper()
{
    //phone=resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null, ContactsContract.CommonDataKinds.Phone._ID+"="+id,null,null);
      phone=resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null, null,null,null);
    while(phone.moveToNext()){
        //获取id，通过索引获取相应的值
        int idIndex = phone.getColumnIndex(ContactsContract.Contacts._ID);    //获取ID值的索引
        int displayNameIndex =phone.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);//获取姓名索引
       //获取id，通过索引获取相应的值
        int number= phone.getInt(idIndex);
        String name  = phone.getString(displayNameIndex);

        sd.append(name+":"+number+"\n");

    }

    sd.notify();



}

    }